package com.pzh.guli.service.edu.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pzh.guli.service.edu.feign.VodService;
import com.pzh.guli.service.edu.mapper.VideoMapper;
import com.pzh.guli.service.edu.pojo.Chapter;
import com.pzh.guli.service.edu.mapper.ChapterMapper;
import com.pzh.guli.service.edu.pojo.Video;
import com.pzh.guli.service.edu.pojo.vo.ChapterVo;
import com.pzh.guli.service.edu.pojo.vo.VideoVo;
import com.pzh.guli.service.edu.service.IChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xzh
 * @since 2022-05-11
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements IChapterService {
    @Resource
    private ChapterMapper chapterMapper;
    @Resource
    private VideoMapper videoMapper;
    @Autowired
    private VodService vodService;
    @Override
    @Transactional
    public List<ChapterVo> nestList(String courseId) {
        //获取章节（根据id）
        QueryWrapper<Chapter> chapterQueryWrapper=new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        chapterQueryWrapper.orderByAsc("sort","id");
        List<Chapter> chapters = chapterMapper.selectList(chapterQueryWrapper);
        //获取video（根据id）
        QueryWrapper<Video> videoQueryWrapper=new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        videoQueryWrapper.orderByAsc("sort","id");
        List<Video> videos = videoMapper.selectList(videoQueryWrapper);
        //组装章节列表

        //todo 这里双层for循环，加属性拷贝，可能会出现性能问题，需要仔细看下是不是最优实现（应该问题不大，但是这种最好加上redis缓存）
        List<ChapterVo> chapterVoList=new ArrayList<>();
        for (int i = 0; i <chapters.size(); i++) {
            Chapter chapter=chapters.get(i);
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            chapterVoList.add(chapterVo);
            List<VideoVo> videoVoList=new ArrayList<>();
            for (int j = 0; j < videos.size(); j++) {
                Video video = videos.get(j);
                if (chapter.getId().equals(video.getChapterId())){
                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
        }
        return chapterVoList;
    }
    //删除章节
    @Override
    @Transactional
    public boolean removeChapterById(String chapterId) {

        //删除前查询出list集合ids
        QueryWrapper<Video> videoQueryWrapper=new QueryWrapper<>();
        videoQueryWrapper.select("video_source_id");
        videoQueryWrapper.eq("chapter_id",chapterId);
        List<Map<String, Object>> maps = videoMapper.selectMaps(videoQueryWrapper);
        ArrayList<String> ids=new ArrayList();
        for (Map<String, Object> map : maps) {
            String vodId= (String) map.get("video_source_id");
            ids.add(vodId);
        }
        //远程调用
        vodService.removeMoreVideoByIds(ids);
        //在删除和chapter有关的video
        QueryWrapper<Video> videoWrapper=new QueryWrapper();
        videoWrapper.eq("chapter_id",chapterId);
        videoMapper.delete(videoWrapper);
        //远程调用删除云端服务
        //在删除chapter
        QueryWrapper<Chapter> chapterQueryWrapper=new QueryWrapper();
        chapterQueryWrapper.eq("id",chapterId);
        int delete = chapterMapper.delete(chapterQueryWrapper);
        return delete!=0;
    }
}
